package cn.edu.njust.protobuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ProtobufClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(
                Message.WrappedMessage
                        .newBuilder()
                        .setMessageType(Message.WrappedMessage.MessageType.REQUEST)
                        .setRequest(
                                Message.Request
                                        .newBuilder()
                                        .setMessage("客户端请求")
                                        .build()
                        )
                        .build()
        );
        log.info("向服务端发送请求");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Message.WrappedMessage message = (Message.WrappedMessage) msg;
        switch (message.getMessageType()) {
            case RESPONSE:
                log.info("收到服务端响应：status={}, message={}", message.getResponse().getStatus(), message.getResponse().getMessage());
                break;
        }
    }
}
